<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
    <el-form-item label="商品名称" prop="commodityName">
      <el-input v-model="dataForm.commodityName" placeholder="商品名称"></el-input>
    </el-form-item>
      <el-form-item label="商品供应商" prop="commoditySupplier">
        <el-input v-model="dataForm.commoditySupplier" placeholder="商品供应商"></el-input>
      </el-form-item>
      <el-form-item label="商品规格" prop="commoditySpecifications">
        <el-input v-model="dataForm.commoditySpecifications" placeholder="商品规格"></el-input>
      </el-form-item>
      <el-form-item label="商品产地" prop="commodityProducing">
        <el-input v-model="dataForm.commodityProducing" placeholder="商品产地"></el-input>
      </el-form-item>
    <el-form-item label="商品价格" prop="commodityPrice">
      <el-input-number v-model="dataForm.commodityPrice" placeholder="商品价格"></el-input-number>
    </el-form-item>
    <el-form-item label="商品种类" prop="commodityType">
      <el-select v-model="dataForm.commodityType" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="商品包装日期" prop="packagingDate">
      <el-date-picker v-model="dataForm.packagingDate" placeholder="商品包装日期"></el-date-picker>
    </el-form-item>
    <el-form-item label="商品保质期时间" prop="lifeDate">
      <el-date-picker v-model="dataForm.lifeDate" placeholder="商品保质期时间"></el-date-picker>
    </el-form-item>

    <el-form-item label="商品图片地址" prop="commodityImage">
      <el-upload
        v-model="dataForm.commodityImage"
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="successHandle"
        :before-upload="beforeUploadHandle">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        options: [],
        url: '',
        imageUrl: '',
        visible: false,
        dataForm: {
          id: 0,
          entId: '',
          commodityName: '',
          commodityPrice: '',
          commoditySupplier: '',
          commodityType: '',
          packagingDate: '',
          lifeDate: '',
          commoditySpecifications: '',
          commodityImage: '',
          commodityProducing: ''
        },
        dataRule: {
          commodityName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          commodityPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          commoditySupplier: [
            { required: true, message: '商品供应商不能为空', trigger: 'blur' }
          ],
          commodityType: [
            { required: true, message: '商品种类不能为空', trigger: 'blur' }
          ],
          packagingDate: [
            { required: true, message: '商品包装日期不能为空', trigger: 'blur' }
          ],
          lifeDate: [
            { required: true, message: '商品保质期时间不能为空', trigger: 'blur' }
          ],
          commoditySpecifications: [
            { required: true, message: '商品规格不能为空', trigger: 'blur' }
          ],
          commodityProducing: [
            { required: true, message: '商品产地不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.url = this.$http.adornUrl(`/sys/minio/upload?token=${this.$cookie.get('Ent-token')}`)
    },
    methods: {
      successHandle (response, res, file) {
        this.imageUrl = window.URL.createObjectURL(res.raw)
        if (response && response.code === 0) {
          if (this.num === this.successNum) {
            this.dataForm.commodityImage = response.url
            this.$message.info('上传成功')
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      beforeUploadHandle (file) {
        const isLt2M = file.size / 1024 / 1024 < 2
        if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
          this.$message.error('只支持jpg、png、gif格式的图片！')
          return false
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
          return false
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.imageUrl = ''
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcommodity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.entId = data.entCommodity.entId
                this.dataForm.commodityName = data.entCommodity.commodityName
                this.dataForm.commodityPrice = data.entCommodity.commodityPrice
                this.dataForm.commoditySupplier = data.entCommodity.commoditySupplier
                this.dataForm.commodityType = data.entCommodity.commodityType
                this.dataForm.packagingDate = data.entCommodity.packagingDate
                this.dataForm.lifeDate = data.entCommodity.lifeDate
                this.dataForm.commoditySpecifications = data.entCommodity.commoditySpecifications
                this.dataForm.commodityImage = data.entCommodity.commodityImage
                this.dataForm.commodityProducing = data.entCommodity.commodityProducing
                this.imageUrl = data.entCommodity.commodityImage
              }
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/app/dictitem/getListById/commodity_type`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.options = data.list
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entcommodity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'entId': this.dataForm.entId,
                'commodityName': this.dataForm.commodityName,
                'commodityPrice': this.dataForm.commodityPrice,
                'commoditySupplier': this.dataForm.commoditySupplier,
                'commodityType': this.dataForm.commodityType,
                'packagingDate': this.dataForm.packagingDate,
                'lifeDate': this.dataForm.lifeDate,
                'commoditySpecifications': this.dataForm.commoditySpecifications,
                'commodityImage': this.dataForm.commodityImage,
                'commodityProducing': this.dataForm.commodityProducing
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
